package sun.still.alive;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Logger;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RhinoServlet
 */
public class RhinoServlet extends HttpServlet {

	private static final long serialVersionUID = 7313374L;

	private static final ScriptEngineManager mgr = new ScriptEngineManager();
	private static boolean DEBUG;
	private static final Logger LOG = Logger//
			.getLogger(RhinoServlet.class.getSimpleName());
	private String PATH;

	@Override
	public void init(ServletConfig config) throws ServletException {
		PATH = config.getServletContext().getRealPath(".");

		String dbg = config.getInitParameter("debug");
		if ("true".equals(dbg))
			DEBUG = true;
		if (DEBUG)
			LOG.info("Log is enabled");

		String ssPath = config.getInitParameter("serversidepath");
		if (ssPath != null && new File(PATH + ssPath).exists())
			PATH += ssPath;
		else
			PATH += "/WEB-INF";
		if (DEBUG)
			LOG.info("the server side script path is :[" + PATH + "]");
	}

	public void service(ServletRequest request, ServletResponse response)
			throws ServletException, IOException {
		try {
			HttpServletRequest req = (HttpServletRequest) request;
			HttpServletResponse res = (HttpServletResponse) response;
			String path = req.getPathInfo();

			ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
			// ScriptContext ctx = jsEngine.getContext();

			File f = new File(PATH + path);
			if (DEBUG)
				LOG.info("requesting path: [" + path + "]");
			if (f.exists() && f.isFile()) {
				res.setContentType("text/html");
				jsEngine.eval(new FileReader(f));
				Invocable inv = (Invocable) jsEngine;
				String metodo = req.getMethod().toLowerCase();
				inv.invokeFunction(metodo, req, res);
			} else {
				if (DEBUG)
					LOG.warning("script [" + path + "] not found");
				res.setStatus(404);
			}
		} catch (ScriptException e) {
			throw new ServletException(e);
		} catch (NoSuchMethodException e) {
			throw new ServletException(e);
		}
	};
}
